<?php 
	/**
		- Task	: Websevice with NuSoap
		- Auth	: Leeseawuyhs
		- Ver	: 1.0
		- Company:	Softech
		- Description: 

	*/
	require_once('lib/nusoap.php');
	require_once('lib/connects_db.php');
	require_once('lib/Login_Os.php');
	
	//$ns : namespace
	$ns_urn 	= 'urn:php_adroid';
	//Server
	$server = new soap_server();
	
	// setup WSDL file, a WSDL file can contain multiple services
	$server->configureWSDL('php_adroid',$ns_urn);
	
	$server->wsdl->schemaTargetNamespace=$ns_urn;
	
	
	/*
		Task	: Hello
	----------------------------------------------*/
	$server->register('hello',                // method name
		array('name' => 'xsd:string'),        // input parameters
		array('return' => 'xsd:string'),      // output parameters
		$ns_urn,                      			 // namespace
		"$ns_urn.#hello",                // soapaction
		'rpc',                                // style
		'encoded',                            // use
		'Says hello to the caller'            // documentation
	);

	function hello($name) {
			return 'Hello, ' . $name;
	}
	//End Hello///////////////////////////////////////////////////////////////////////////
	//------------------------------------------------------------------------------------
	/*
		Task: Get 10 question
	*/
	// Register the data structures question by the service
	$server->wsdl->addComplexType(
		'InfoUser',
		'complexType',
		'struct',
		'all',
		'',
		array(
			'username' => array('name' => 'username', 'type' => 'xsd:string'),
			'password' => array('name' => 'password', 'type' => 'xsd:string'),
		)
	);
	$server->wsdl->addComplexType(
		'GetQuestion',
		'complexType',
		'struct',
		'all',
		'',
		array(
			'id' 				=> array('name' => 'id', 'type' => 'xsd:int'),
			'name' 			=> array('name' => 'name', 'type' => 'xsd:string'),
			'question_a' 	=> array('name' => 'question_a', 'type' => 'xsd:string'),
			'question_b' 	=> array('name' => 'question_b', 'type' => 'xsd:string'),
			'question_c' 	=> array('name' => 'question_c', 'type' => 'xsd:string'),
			'question_d' 	=> array('name' => 'question_d', 'type' => 'xsd:string'),
			'answer' 		=> array('name' => 'answer', 'type' => 'xsd:int'),
		)
	);
	
	$server->register('listQuestions',                // method name
		//array('info_user' => 'tns:InfoUser'),		// input parameters
		array('username' => 'xsd:string','password' => 'xsd:string'), 	// input parameters
		//array('return' => 'tns:GetQuestion'),	// output parameters
		array('return'=>'xsd:string'),
		$ns_urn,                      			 // namespace
		"$ns_urn.#listQuestions",                // soapaction
		'rpc',                                // style
		'encoded',                            // use
		'Get 10 Question'            // documentation
	);

	function listQuestions($username,$password){//$info_user = null) {
	//	global $server;
		//Login system
			$user =$username;// $info_user['username'];
			$password = $password;//$info_user['password'];

		//Skip Get login system
		$login_os = new Login_Os();
		if($login_os->login($user,$password) == false){
			return 'You have entered an invalid username/password combination';	
		}
		return 'User: '.$user .' Pass: '.$password;
		//Get questions
		$conn_db = new Connect_Db();
		$conn_db->conn_db();
		$query ="SELECT *
					FROM questions
					LIMIT 0 , 1;";
		$result = mysqli_query($conn,$query) or die(mysqli_error($conn));
		if(mysqli_num_rows($result) == 0){
				return array('name'=>"Data empty");
		}
		else{
			$set_questions = array();
			$i =0;
			while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
				if($i==0){
					$set_questions['id']				=  $row['id'];
					$set_questions['name']			=  $row['name'];
					$set_questions['question_a']	=  $row['question_a'];
					$set_questions['question_b']	=  $row['question_b'];
					$set_questions['question_c']	=  $row['question_c'];
					$set_questions['question_d']	=  $row['question_d'];
					$set_questions['answer']			=  $row['answer'];
					$i++;
				}
			}
		}
		//var_dump($set_questions);
		return $set_questions;
	}
	//End listUsers///////////////////////////////////////////////////////////////////////////
	//------------------------------------------------------------------------------------
	
	// service the methods 
	// Use the request to (try to) invoke the service
	$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
	$server->service($HTTP_RAW_POST_DATA);


?>